package zdori.swing;

import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;



public class SwingPopupMenu_t extends JPopupMenu
{
	JPopupMenu popup=null;
	
	public SwingPopupMenu_t(JPanel c)
	{
		//팝업메뉴 생성
		popup=new JPopupMenu();
		addPopup(c, popup);
		JMenuItem m1=new JMenuItem("new");
		popup.add(m1);
		c.addMouseListener(new PopupMenuListener());
		//c.addMouseListener(new PopupMenuListener(popup));
		//SwingMain의 contentPane(JPanel)에 
		//popuplistener를 extends로 등록
	
	}
	
	class PopupMenuListener extends MouseAdapter
	{
		//JPopupMenu popup=null;
		//public PopupMenuListener(JPopupMenu popup) 
		//{this.popup=popup;}
		
		public void mousePressed(MouseEvent e){}
		public void mouseReleased(MouseEvent e){}
		public void showMenu(MouseEvent e){}
		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
